/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import negocio.Usuario;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author EQUIPO
 */
 /**
     * Creates a new instance of MbLogin
     */
@ManagedBean
@RequestScoped
public class MbLogin implements Serializable
{
 
    private String usuario;
    private String contrasenia;
    private final HttpServletRequest httpServletRequest;
    private final FacesContext faceContext;
    private FacesMessage facesMessage;
     
    @ManagedProperty("#{usuarioController}")
    private UsuarioController usuarioController;
    
    
    public MbLogin()
    {
        faceContext=FacesContext.getCurrentInstance();
        httpServletRequest=(HttpServletRequest)faceContext.getExternalContext().getRequest();
    }
     
    
    public String login() throws SQLException
    {
        
        
        List<Usuario> items1=usuarioController.getItems();
        for(int i=0;i<items1.size();i++){
            if(usuario.equals(items1.get(i).getUsername()) && contrasenia.equals(items1.get(i).getPassword()))
            {
                if(items1.get(i).getAdministrador()){
                    httpServletRequest.getSession().setAttribute("sessionUsuario", usuario);
                    facesMessage=new FacesMessage(FacesMessage.SEVERITY_INFO, "Acceso Correcto", null);
                    faceContext.addMessage(null, facesMessage);
                    return "/graficoobjetivos";
                }else{
                    httpServletRequest.getSession().setAttribute("sessionUsuario", usuario);
                    facesMessage=new FacesMessage(FacesMessage.SEVERITY_INFO, "Acceso Correcto", null);
                    faceContext.addMessage(null, facesMessage);
                    return "/graficoobjetivosuser";
                }
            }
        }
        facesMessage=new FacesMessage(FacesMessage.SEVERITY_ERROR, "Usuario o contraseña incorrecto", null);
        faceContext.addMessage(null, facesMessage);
        return "/login";
    }
 
    public String getUsuario() {
        return usuario;
    }
 
    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }
 
    public String getContrasenia() {
        return contrasenia;
    }
 
    public void setContrasenia(String contrasenia) {
        this.contrasenia = contrasenia;
    }   
    
    public void setUsuarioController(UsuarioController usuarioController){
        this.usuarioController=usuarioController;
    }
}
